rm(list = setdiff(ls(), lsf.str()))

### British Election ------------
load("Study 1/Altered Data/Study1_UK_BES.RData")
#W6 Run logistic regression model: government - opposition
mlogit1 <- multinom(w6_UKIP_gov_opposition ~zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) +female +Age+ Age_missing + Ed_GSCE_DG + Ed_GSCE_AC + Ed_A_level + Ed_Undergraduate + Ed_Postgrad + income + income_missing + zero1(w6_pol_cynicism) + zero1(w4_immiatt) + zero1(w6_redistribution), data=data_BES)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(mlogit1))
mlogit.conf<-as.data.frame(exp(confint(mlogit1)))

#Government
uk_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(uk_gov)<- c("odds","low","high")
uk_gov$party<-"Government"
uk_gov$country<-"United Kingdom"
uk_gov$sample<-"UK Election 2015"

#Opposition
uk_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(uk_opp)<- c("odds","low","high")
uk_opp$party<-"Opposition"
uk_opp$country<-"United Kingdom"
uk_opp$sample<-"UK Election 2015"

### Germany ------------------
load("Study 1/Altered Data/Study1_Germany.RData")
vote_multinom<-multinom(vote_populist_mlogit_govopposition~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age + income+income_missing + education + econ_cons+social_cons, data=data,  weights = z000011a)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(vote_multinom))
mlogit.conf<-as.data.frame(exp(confint(vote_multinom)))

#Government
germany_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(germany_gov)<- c("odds","low","high")
germany_gov$party<-"Government"
germany_gov$country<-"Germany"
germany_gov$sample<-"GESIS 2017"

#Opposition
germany_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(germany_opp)<- c("odds","low","high")
germany_opp$party<-"Opposition"
germany_opp$country<-"Germany"
germany_opp$sample<-"GESIS 2017"

### Denmark---------------
load("Study 1/Altered Data/Study1_Denmark.RData")
mlogit1 <- multinom(vote_populist10_intention_govopp ~ zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age+as.factor(v2010_education)+income + income_missing + zero1(econ_cons) + zero1(social) + zero1(cynicism), data=data)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(mlogit1))
mlogit.conf<-as.data.frame(exp(confint(mlogit1)))

#Government
denmark_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(denmark_gov)<- c("odds","low","high")
denmark_gov$party<-"Government"
denmark_gov$country<-"Denmark"
denmark_gov$sample<-"2010"

#Opposition
denmark_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(denmark_opp)<- c("odds","low","high")
denmark_opp$party<-"Opposition"
denmark_opp$country<-"Denmark"
denmark_opp$sample<-"2010"

### Netherlands 2012---------------
load("Study 1/Altered Data/Study1_NL_12.RData")

mlogit1 <- multinom(vote_pvv_multinom_govopp ~  zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + as.factor(education) +income + income_missing + zero1(econ_cons11) + zero1(anti_immi11) + zero1(cynicism11), data=data)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(mlogit1))
mlogit.conf<-as.data.frame(exp(confint(mlogit1)))

#Government
nl12_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(nl12_gov)<- c("odds","low","high")
nl12_gov$party<-"Government"
nl12_gov$country<-"Netherlands 2012"
nl12_gov$sample<-"2012"

#Opposition
nl12_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(nl12_opp)<- c("odds","low","high")
nl12_opp$party<-"Opposition"
nl12_opp$country<-"Netherlands 2012"
nl12_opp$sample<-"2012"

### Netherlands 2014--------------
load("Study 1/Altered Data/Study1_NL_14.RData")
mlogit1<-multinom(w1_pvv_govopp~zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + sex + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_cyn) + zero1(w1_immi), data=data_sub)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(mlogit1))
mlogit.conf<-as.data.frame(exp(confint(mlogit1)))

#Government
nl14_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(nl14_gov)<- c("odds","low","high")
nl14_gov$party<-"Government"
nl14_gov$country<-"Netherlands 2014"
nl14_gov$sample<-"2014"

#Opposition
nl14_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(nl14_opp)<- c("odds","low","high")
nl14_opp$party<-"Opposition"
nl14_opp$country<-"Netherlands 2014"
nl14_opp$sample<-"2014"

### Netherlands 2017-----------
load("Study 1/Altered Data/Study1_NL_17.RData")
mlogit1<-multinom(populist_govopp ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age+ as.factor(education) + income + income_missing  + zero1(econ_cons) + zero1(anti_immi) + zero1(cynicism) , data=data)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(mlogit1))
mlogit.conf<-as.data.frame(exp(confint(mlogit1)))

#Government
nl17_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(nl17_gov)<- c("odds","low","high")
nl17_gov$party<-"Government"
nl17_gov$country<-"Netherlands 2017"
nl17_gov$sample<-"2017"

#Opposition
nl17_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(nl17_opp)<- c("odds","low","high")
nl17_opp$party<-"Opposition"
nl17_opp$country<-"Netherlands 2017"
nl17_opp$sample<-"2017"


#Pool UK - British Election Studies -----------------------
load("Study 1/Altered Data/Study1_UK_BES.RData")
data_BES$id <- sample(21811, size = nrow(data_BES))
data_BES$id <- 300000+data_BES$id 
data_BES$education<-ifelse(data_BES$Ed_GSCE_DG==1,1,0)
data_BES$education[data_BES$Ed_GSCE_AC==1]=2
data_BES$education[data_BES$Ed_A_level==1]=3
data_BES$education[data_BES$Ed_Undergraduate==1]=4
data_BES$education[data_BES$Ed_Postgrad==1]=5

data_BES <- data_BES[,which(colnames(data_BES)%in%c("id", "w6_UKIP_gov_opposition", "agre", "open", "con","ext","neu","female", "Age", "education", "income", "income_missing", "w6_pol_cynicism", "w4_immiatt", "w6_redistribution"))]
data_BES$study <- 2
colnames(data_BES)[colnames(data_BES)=="w6_UKIP_gov_opposition"] <- "populist_multinom"
colnames(data_BES)[colnames(data_BES)=="sex"] <- "female"
colnames(data_BES)[colnames(data_BES)=="Age"] <- "age"
colnames(data_BES)[colnames(data_BES)=="w6_pol_cynicism"] <- "cynicism"
colnames(data_BES)[colnames(data_BES)=="w4_immiatt"] <- "social_cons"
colnames(data_BES)[colnames(data_BES)=="w6_redistribution"] <- "econ_cons"
data_BES$education<-zero1(data_BES$education)
data_BES$econ_cons_missing<-0
data_BES$social_cons_missing<-0
data_BES$cynicism_missing<-0
data_BES$lr_placement<-2
data_BES$lr_placement_missing<-1

data_BES$agre<-zero1(data_BES$agre)
data_BES$open<-zero1(data_BES$open)
data_BES$con<-zero1(data_BES$con)
data_BES$ext<-zero1(data_BES$ext)
data_BES$neu<-zero1(data_BES$neu)
data_BES$cynicism<-zero1(data_BES$cynicism)
data_BES$social_cons<-zero1(data_BES$social_cons)
data_BES$econ_cons<-zero1(data_BES$econ_cons)

data_BES$populist_multinom<-ifelse(data_BES$populist_multinom=="Don't know", NA, data_BES$populist_multinom)

#Pool Germany---------------------------
load("Study 1/Altered Data/Study1_Germany.RData")
data_germany <- data[,which(colnames(data)%in%c("z000001a", "vote_populist_mlogit_govopposition", "agre", "open", "con", "ext","neu", "female", "age", "education", "income", "income_missing", "econ_cons", "social_cons"))]
colnames(data_germany)[colnames(data_germany)=="vote_populist_mlogit_govopposition"] <- "populist_multinom"
colnames(data_germany)[colnames(data_germany)=="z000001a"] <- "id"
data_germany$open<- zero1(data_germany$open)
data_germany$con<- zero1(data_germany$con)
data_germany$ext<- zero1(data_germany$ext)
data_germany$agre<- zero1(data_germany$agre)
data_germany$neu<- zero1(data_germany$neu)
data_germany$social_cons<- zero1(data_germany$social_cons)

data_germany$econ_cons_missing<-0
data_germany$social_cons_missing<-0
data_germany$cynicism<-0
data_germany$cynicism_missing<-1
data_germany$lr_placement<-2
data_germany$lr_placement_missing<-1
data_germany$study<-3
data_germany<-as.numeric(data_germany$populist_multinom)

#Pool Denmark-------------------------
load("Study 1/Altered Data/Study1_Denmark.RData")
data_DK <- data[,which(colnames(data)%in%c("vote_populist10_intention_govopp", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "v2010_education", "social", "econ_cons", "cynicism"))]
data_DK$study <- 4

data_DK$id <- sample(6387, size = nrow(data_DK))
data_DK$id <- 900000+data_DK$id 
colnames(data_DK)[colnames(data_DK)=="vote_populist10_intention_govopp"] <- "populist_multinom"
colnames(data_DK)[colnames(data_DK)=="v2010_education"] <- "education"
colnames(data_DK)[colnames(data_DK)=="social"] <- "social_cons"

data_DK$open<- zero1(data_DK$open)
data_DK$con<- zero1(data_DK$con)
data_DK$ext<- zero1(data_DK$ext)
data_DK$agre<- zero1(data_DK$agre)
data_DK$neu<- zero1(data_DK$neu)
data_DK$education<- zero1(data_DK$education)
data_DK$econ_cons<- zero1(data_DK$econ_cons)
data_DK$cynicism<- zero1(data_DK$cynicism)
data_DK$social_cons<- zero1(data_DK$social_cons)
data_DK$econ_cons_missing<-0
data_DK$social_cons_missing<-0
data_DK$cynicism_missing<-0
data_DK$lr_placement<-2
data_DK$lr_placement_missing<-1
data_DK$populist_multinom<-as.numeric(data_DK$populist_multinom)

#Pool NL 2012--------------
load("Study 1/Altered Data/Study1_NL_12.RData")
data_NL12 <- data[,which(colnames(data)%in%c("nomem_encr", "vote_pvv_multinom_govopp", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "education", "cynicism11", "anti_immi11", "econ_cons11"))]
data_NL12$study <- 6
colnames(data_NL12)[colnames(data_NL12)=="nomem_encr"] <- "id"
colnames(data_NL12)[colnames(data_NL12)=="econ_cons11"] <- "econ_cons"
colnames(data_NL12)[colnames(data_NL12)=="anti_immi11"] <- "social_cons"
colnames(data_NL12)[colnames(data_NL12)=="cynicism11"] <- "cynicism"
colnames(data_NL12)[colnames(data_NL12)=="vote_pvv_multinom_govopp"] <- "populist_multinom"

data_NL12$econ_cons<- zero1(data_NL12$econ_cons)
data_NL12$social_cons<- zero1(data_NL12$social_cons)
data_NL12$cynicism<- zero1(data_NL12$cynicism)
data_NL12$education<- zero1(data_NL12$education)
data_NL12$open<- zero1(data_NL12$open)
data_NL12$con<- zero1(data_NL12$con)
data_NL12$ext<- zero1(data_NL12$ext)
data_NL12$agre<- zero1(data_NL12$agre)
data_NL12$neu<- zero1(data_NL12$neu)
data_NL12$econ_cons_missing<-0
data_NL12$social_cons_missing<-0
data_NL12$cynicism_missing<-0
data_NL12$lr_placement<-2
data_NL12$lr_placement_missing<-1
data_NL12$populist_multinom<-as.numeric(data_NL12$populist_multinom)

#Pool Netherlands EU elections 2014----------------------
load("Study 1/Altered Data/Study1_NL_14.RData")
data_sub$education<-ifelse(data_sub$prep_secon==1,1,0)
data_sub$education[data_sub$highschool==1]=2
data_sub$education[data_sub$secondary_vocation==1]=3
data_sub$education[data_sub$pre_uni==1]=4
data_sub$education[data_sub$college==1]=5
data_sub$education[data_sub$uni==1]=6
NL_15 <- data_sub[,which(colnames(data_sub)%in%c("w1_pvv_govopp", "w5_agre", "w5_open", "w5_con","w5_extra","w5_neu","female", "age", "education", "income", "income_missing", "w1_cyn", "w1_immi"))]

NL_15$id <- sample(1174, size = nrow(NL_15))
NL_15$id <- 950000+NL_15$id 
colnames(NL_15)[colnames(NL_15)=="w1_pvv_govopp"] <- "populist_multinom"
colnames(NL_15)[colnames(NL_15)=="w5_agre"] <- "agre"
colnames(NL_15)[colnames(NL_15)=="w5_open"] <- "open"
colnames(NL_15)[colnames(NL_15)=="w5_con"] <- "con"
colnames(NL_15)[colnames(NL_15)=="w5_extra"] <- "ext"
colnames(NL_15)[colnames(NL_15)=="w5_neu"] <- "neu"
colnames(NL_15)[colnames(NL_15)=="sex"] <- "female"
colnames(NL_15)[colnames(NL_15)=="w1_immi"] <- "social_cons"
colnames(NL_15)[colnames(NL_15)=="w1_cyn"] <- "cynicism"

NL_15$open<- zero1(NL_15$open)
NL_15$con<- zero1(NL_15$con)
NL_15$ext<- zero1(NL_15$ext)
NL_15$agre<- zero1(NL_15$agre)
NL_15$neu<- zero1(NL_15$neu)
NL_15$education<-zero1(NL_15$education)
NL_15$social_cons<-zero1(NL_15$social_cons)
NL_15$cynicism<-zero1(NL_15$cynicism)
NL_15$study <- 7
NL_15$econ_cons <- 2
NL_15$econ_cons_missing <- 1
NL_15$social_cons_missing<-0
NL_15$cynicism_missing<-0
NL_15$lr_placement<-2
NL_15$lr_placement_missing<-1
NL_15$populist_multinom<-as.numeric(NL_15$populist_multinom)

#Pool Netherlands Election 2017 -------------
load("Study 1/Altered Data/Study1_NL_17.RData")
data_NL17 <- data[,which(colnames(data)%in%c("nomem_encr", "populist_govopp", "agre", "open", "con","ext","neu","female", "age", "income", "income_missing", "education", "anti_immi", "econ_cons", "cynicism"))]

data_NL17$study <- 9
data_NL17$education<-zero1(data_NL17$education)
data_NL17$open<- zero1(data_NL17$open)
data_NL17$con<- zero1(data_NL17$con)
data_NL17$ext<- zero1(data_NL17$ext)
data_NL17$agre<- zero1(data_NL17$agre)
data_NL17$neu<- zero1(data_NL17$neu)
colnames(data_NL17)[colnames(data_NL17)=="populist_govopp"] <- "populist_multinom"
colnames(data_NL17)[colnames(data_NL17)=="nomem_encr"] <- "id"
colnames(data_NL17)[colnames(data_NL17)=="econ_cons"] <- "econ_cons"
colnames(data_NL17)[colnames(data_NL17)=="anti_immi"] <- "social_cons"
colnames(data_NL17)[colnames(data_NL17)=="cynicism"] <- "cynicism"
data_NL17$econ_cons<- zero1(data_NL17$econ_cons)
data_NL17$social_cons<- zero1(data_NL17$social_cons)
data_NL17$cynicism<- zero1(data_NL17$cynicism)

data_NL17$econ_cons_missing<-0
data_NL17$social_cons_missing<-0
data_NL17$cynicism_missing<-0
data_NL17$lr_placement<-2
data_NL17$lr_placement_missing<-1
data_NL17$populist_multinom<-as.numeric(data_NL17$populist_multinom)


#Pool Merge data---------------
data_meta <- rbind(data_BES,  data_germany, data_DK, data_NL12, NL_15, data_NL17)
data_meta$populist_multinom<-as.factor(data_meta$populist_multinom)
levels(data_meta$populist_multinom) <- c("Populist","Government","Opposition")
data_meta<-na.omit(data_meta)

mlogit1 <- multinom(populist_multinom ~ agre + open + con + ext + neu +age + female + education + income + income_missing + social_cons  + econ_cons+ cynicism  + lr_placement + as.factor(study) , data=data_meta)

#calculate odds ratio and confidence intervals
mlogit.odds<-exp(coef(mlogit1))
mlogit.conf<-as.data.frame(exp(confint(mlogit1)))

#Government
pooled_gov<-cbind(mlogit.odds[1,2], mlogit.conf[2,1:2])
colnames(pooled_gov)<- c("odds","low","high")
pooled_gov$party<-"Government"
pooled_gov$country<-"Pooled"
pooled_gov$sample<-"Pooled"

#Opposition
pooled_opp<-cbind(mlogit.odds[2,2], mlogit.conf[2,3:4])
colnames(pooled_opp)<- c("odds","low","high")
pooled_opp$party<-"Opposition"
pooled_opp$country<-"Pooled"
pooled_opp$sample<-"Pooled"


#Appendix A.19 merge data-------------------------
incumbent<-rbind(uk_gov, uk_opp, germany_gov, germany_opp, denmark_gov, denmark_opp, nl12_gov, nl12_opp, nl14_gov, nl14_opp, nl17_gov, nl17_opp, pooled_gov, pooled_opp)

incumbent$country <-factor(incumbent$country, levels=c('United Kingdom', 'Germany',  'Denmark', 'Netherlands 2012', 'Netherlands 2014', 'Netherlands 2017','Pooled'))

incumbent$party <-factor(incumbent$party, levels=c('Opposition', 'Government'))

#Plot figure------------
fig2 <- ggplot(incumbent ,aes(x=party, y=odds))+geom_pointrange(aes(ymin=low,ymax=high), size=1)  + theme_bw()+theme(legend.position="off")+ylab("Odds ratio of voting for government or opposition parties")+xlab("")+geom_hline(yintercept=1,linetype = "dashed", color="red") + coord_flip() + theme(axis.text.x=element_text(size=14), strip.text.y=element_text(size=14)) +  theme(strip.text.y = element_text(angle = 360))+ facet_wrap(country~., ncol=2, scales="free")

ggsave(fig2, file="Figures/fig2_gov_opp.pdf", dpi=900, width = 8, height =9)

